/***
 * Excerpted from "Stripes: and Java Web Development is Fun Again",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/fdstr for more book information.
 ***/
package stripesbook.action.user;

import java.util.Collection;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import stripesbook.model.Contact;

public class ContactListActionBean extends ContactBaseActionBean {

  @Override
  public Object getView() {
    return tag(ContactListView.class).init(getContacts());
  }

  @DefaultHandler
  public void list() {
  }

  public Resolution details() {
    return stream(tag(ContactDetails.class).contact(getContact()));
  }

  public Resolution delete() {
    Contact deleted = getContact();
    contactDao.delete(deleted);
    contactDao.commit();
    return streamContactTable();
  }

  private Collection<Contact> contacts;

  public Collection<Contact> getContacts() {
    if (contacts == null) {
      contacts = getUser().getContacts();
    }
    return contacts;
  }

  public String filter;

  public Resolution findByName() {
    if (filter != null && filter.length() > 0) {
      contacts = contactDao.findByName(filter, getUser());
    }
    return new ForwardResolution(getClass(), "table");
  }
}
